package java_collection.iterator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import static java.lang.System.out;

public class IteratorTest {

	public static void main(String[] args) {
		
		sum();
		del();

	}


	public static void sum() {
		
		List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

		// Declaring an Iterator
		Iterator<Integer> itr = list.iterator();

		// sum of all Array elements
		int sum = 0;

		while (itr.hasNext()) {
			sum = sum + itr.next();
		}
		
		out.println(sum); // 15
	}

	public static void del() {
		
		List<Integer> list = new ArrayList<>();
		// add 1- 5 in list
		for (int i=1; i <= 5; i++) {
			list.add(i);
		}
		
		// Delete Array element with value = 3
		
		Iterator<Integer> itr = list.iterator();
		
		while (itr.hasNext()) {
			if (itr.next() == 3) {
				// Deleting from Iterator,
				// deletes the actual element from list
				itr.remove();
			}
		}
		
		out.println(list);
		//[1, 2, 4, 5]
	}
}
